package com.are.inventario;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class BuscarPlacaActivity extends Activity implements OnClickListener {
	private Button buscar;
	private EditText criterio;
	private String url = "";
	private Placa placa;
	
private ProgressDialog progressDialog;
	
	private final Handler progressHandler = new Handler() {

		public void handleMessage(Message msg) {
			if (msg.obj != null) {
				placa = (Placa) msg.obj;
				ShowMensaje(placa);
			}
			progressDialog.dismiss();
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buscar_placa);
        buscar = (Button)findViewById(R.id.cmdBuscarPlaca);
        buscar.setOnClickListener(this);
        criterio = (EditText)findViewById(R.id.txtCriterioPlaca);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_buscar_placa, menu);
        return true;
    }

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.cmdBuscarPlaca) {
			try {
				LoadData();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				Dialog dialogo = crearDialogoAlerta(e.getMessage());
				dialogo.show();
			}
		}
		
	}
	
	private void LoadData() throws UnsupportedEncodingException {
		progressDialog = ProgressDialog.show(BuscarPlacaActivity.this, "",
				"Por favor espere mientras se envian los datos...", true);
		String key = criterio.getText().toString();
		this.url = "http://siscos.fcr.com.co:8080/AreApp/XmlPlaca?key=" + URLEncoder.encode(key, "ISO-8859-1");;
		
		new Thread(new Runnable() {
			public void run() {
				XmlParsePlaca parse = new XmlParsePlaca(url);
				Message msg = progressHandler.obtainMessage();
				msg.obj = parse.parse();
				progressHandler.sendMessage(msg);
			}
		}).start();
	}

	private Dialog crearDialogoAlerta(String mensaje) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Informacion");
		builder.setMessage(mensaje);
		builder.setCancelable(false);
		builder.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				dialog.cancel();
			}
		});		
		return builder.create();
	}
	
	private void ShowMensaje(Placa data) {
		if (!data.getCodigo().equals("")) {
			//AlertDialog alert =  (AlertDialog) this.crearDialogoAlerta("Transformador encontrado " + data.getCodigo());
			//alert.show();
			
			Intent intent = new Intent();
			intent.putExtra("codigo", data.getCodigo());
			intent.putExtra("matricula",data.getMatricula());
			intent.putExtra("latitud",data.getLatitud());
			intent.putExtra("longitud",data.getLongitud());
			intent.putExtra("peso",data.getPeso());
			intent.putExtra("liquido",data.getPliquido());
			intent.putExtra("pais",data.getPais());
			intent.putExtra("anio",data.getAnio());
			
			setResult(RESULT_OK, intent);
			finish();
			
		} else { // error al confirmar la orden
			AlertDialog alert =  (AlertDialog) this.crearDialogoAlerta("Placa de transformador no encontrada");
			alert.show();
		}

	}

    
}
